package mbms.mobilebudget.overview;

import java.util.List;

import mbms.mobilebudget.bankaccount.BankAccountManager;
import mbms.mobilebudget.creditCard.CreditCardManager;
import mbms.mobilebudget.entity.BankAccount;
import mbms.mobilebudget.entity.CreditCard;
import mbms.mobilebudget.entity.EnumValues.CurrencyType;
import mbms.mobilebudget.entity.Exchange;
import mbms.mobilebudget.entity.Expense;
import mbms.mobilebudget.entity.Income;

public class Overview {
	
	private Double totalBudget;
	private Double totalIncome;
	private Double totalExpense;
	
	private Double creditCardTotalExpenditure;
	private Double bankAccountTotalExpenditure;
	
	private Double creditCardTotalIncome;
	private Double bankAccountTotalIncome;
	
	private Double totalSalaryIncome;
	private Double totalRentIncome;
	private Double totalBonusIncome;
	
	private Double totalFoodEatingExpenditure;
	private Double totalTransportationExpenditure;
	private Double totalAmusementExpenditure;
	
	private Double dollarCurrencyRate;
	private Double euroCurrencyRate;
	private Double goldCurrencyRate;
	
	public Overview(BankAccountManager bankAccountManager, CreditCardManager creditCardManager, List<BankAccount> bankAccountList, List<CreditCard> creditCardList, List<Exchange> rates) {
		initializeFields();
		if(rates!= null && !rates.isEmpty()) {
			dollarCurrencyRate = rates.get(0).getDollar();
			euroCurrencyRate = rates.get(0).getEuro();
			goldCurrencyRate = rates.get(0).getGold();
		} 
		
		for (CreditCard creditCard : creditCardList) {
			Double currentyRate = getCurrentyRate(creditCard.getCurrencyType());
			
			setCreditCardTotalExpenditure(getCreditCardTotalExpenditure()
					+ creditCard.getTotalExpenseAmount() * currentyRate);
			setCreditCardTotalIncome(getCreditCardTotalIncome() + creditCard.getTotalIncomeAmount() * currentyRate);
			
			List<Income> incomeListOfCreditCard = creditCardManager.getIncomeListOfCreditCard(creditCard);
			updateIncomeValues(incomeListOfCreditCard, currentyRate);
			
			List<Expense> expenseList = creditCardManager.getExpenseListOfCreditCard(creditCard);
			updateExpenseValues(expenseList, currentyRate);
		}
		
		for (BankAccount bankAccount : bankAccountList) {
			Double currentyRate = getCurrentyRate(bankAccount.getCurrencyType());
			
			setBankAccountTotalExpenditure(getBankAccountTotalExpenditure() + bankAccount.getTotalExpenseAmount() * currentyRate);
			setBankAccountTotalIncome(getBankAccountTotalIncome() + bankAccount.getTotalIncomeAmount() * currentyRate);
			
			
			List<Income> incomeListOfBankAccount = bankAccountManager.getIncomeListOfBankAccount(bankAccount);
			updateIncomeValues(incomeListOfBankAccount, currentyRate);
			
			List<Expense> expenseList = bankAccountManager.getExpenseListOfBankAccount(bankAccount);
			updateExpenseValues(expenseList, currentyRate);
		}
		
		setTotalIncome(getCreditCardTotalIncome() + getBankAccountTotalIncome());
		setTotalExpense(getCreditCardTotalExpenditure() + getBankAccountTotalExpenditure());
		setTotalBudget(getTotalIncome() - getTotalExpense());
	}

	private void initializeFields() {
		 totalBudget= 0.0;
		 totalIncome= 0.0;
		 totalExpense= 0.0;

		 creditCardTotalExpenditure= 0.0;
		 bankAccountTotalExpenditure= 0.0;

		 creditCardTotalIncome= 0.0;
		 bankAccountTotalIncome= 0.0;

		 totalSalaryIncome= 0.0;
		 totalRentIncome= 0.0;
		 totalBonusIncome= 0.0;

		 totalFoodEatingExpenditure= 0.0;
		 totalTransportationExpenditure= 0.0;
		 totalAmusementExpenditure= 0.0;

		 dollarCurrencyRate= 0.0;
		 euroCurrencyRate= 0.0;
		 goldCurrencyRate= 0.0;
	}

	private Double getCurrentyRate(CurrencyType currencyType) {
		switch (currencyType) {
			case TL:
				return 1.0;
			case EURO:
				return getEuroCurrencyRate();
			case DOLLAR:
				return getDollarCurrencyRate();
			case GOLD:
				return getGoldCurrencyRate()/28.35;
		}
		
		return null;
	}

	private void updateExpenseValues(List<Expense> expenseList , Double currencyRate) {
		for (Expense expense : expenseList) {
			switch (expense.getExpenseCategory()) {
				case FOOD_EATING:
					setTotalFoodEatingExpenditure(getTotalFoodEatingExpenditure()
							+ expense.getAmount() * currencyRate);
					break;
				case TRANSPORTATION:
					setTotalTransportationExpenditure(getTotalTransportationExpenditure()
							+ expense.getAmount() * currencyRate);
					break;
				case AMUSEMENT:
					setTotalAmusementExpenditure(getTotalAmusementExpenditure()
							+ expense.getAmount() * currencyRate);
					break;
				default:
					break;
			}
		}
	}

	private void updateIncomeValues(List<Income> incomeListOfBankAccount, Double currencyRate) {
		for (Income income : incomeListOfBankAccount) {
			switch (income.getIncomeCategory()) {
			case SALARY:
				setTotalSalaryIncome(getTotalSalaryIncome() + income.getAmount() * currencyRate);
				break;
			case RENT:
				setTotalRentIncome(getTotalRentIncome() + income.getAmount() * currencyRate);
			break;
			case BONUS:
				setTotalBonusIncome(getTotalBonusIncome() + income.getAmount() * currencyRate);
			break;
			default:
				break;
			}
		}
	}

	public Double getTotalBudget() {
		return totalBudget;
	}

	public void setTotalBudget(Double totalBudget) {
		this.totalBudget = totalBudget;
	}

	public Double getTotalIncome() {
		return totalIncome;
	}

	public void setTotalIncome(Double totalIncome) {
		this.totalIncome = totalIncome;
	}

	public Double getTotalExpense() {
		return totalExpense;
	}

	public void setTotalExpense(Double totalExpense) {
		this.totalExpense = totalExpense;
	}

	public Double getCreditCardTotalExpenditure() {
		return creditCardTotalExpenditure;
	}

	public void setCreditCardTotalExpenditure(Double creditCardTotalExpenditure) {
		this.creditCardTotalExpenditure = creditCardTotalExpenditure;
	}

	public Double getBankAccountTotalExpenditure() {
		return bankAccountTotalExpenditure;
	}

	public void setBankAccountTotalExpenditure(
			Double bankAccountTotalExpenditure) {
		this.bankAccountTotalExpenditure = bankAccountTotalExpenditure;
	}

	public Double getCreditCardTotalIncome() {
		return creditCardTotalIncome;
	}

	public void setCreditCardTotalIncome(Double creditCardTotalIncome) {
		this.creditCardTotalIncome = creditCardTotalIncome;
	}

	public Double getBankAccountTotalIncome() {
		return bankAccountTotalIncome;
	}

	public void setBankAccountTotalIncome(Double bankAccountTotalIncome) {
		this.bankAccountTotalIncome = bankAccountTotalIncome;
	}

	public Double getTotalSalaryIncome() {
		return totalSalaryIncome;
	}

	public void setTotalSalaryIncome(Double totalSalaryIncome) {
		this.totalSalaryIncome = totalSalaryIncome;
	}

	public Double getTotalRentIncome() {
		return totalRentIncome;
	}

	public void setTotalRentIncome(Double totalRentIncome) {
		this.totalRentIncome = totalRentIncome;
	}

	public Double getTotalBonusIncome() {
		return totalBonusIncome;
	}

	public void setTotalBonusIncome(Double totalBonusIncome) {
		this.totalBonusIncome = totalBonusIncome;
	}

	public Double getTotalFoodEatingExpenditure() {
		return totalFoodEatingExpenditure;
	}

	public void setTotalFoodEatingExpenditure(Double totalFoodEatingExpenditure) {
		this.totalFoodEatingExpenditure = totalFoodEatingExpenditure;
	}

	public Double getTotalTransportationExpenditure() {
		return totalTransportationExpenditure;
	}

	public void setTotalTransportationExpenditure(
			Double totalTransportationExpenditure) {
		this.totalTransportationExpenditure = totalTransportationExpenditure;
	}

	public Double getTotalAmusementExpenditure() {
		return totalAmusementExpenditure;
	}

	public void setTotalAmusementExpenditure(Double totalAmusementExpenditure) {
		this.totalAmusementExpenditure = totalAmusementExpenditure;
	}

	public Double getDollarCurrencyRate() {
		return dollarCurrencyRate;
	}

	public void setDollarCurrencyRate(Double dollarCurrencyRate) {
		this.dollarCurrencyRate = dollarCurrencyRate;
	}

	public Double getEuroCurrencyRate() {
		return euroCurrencyRate;
	}

	public void setEuroCurrencyRate(Double euroCurrencyRate) {
		this.euroCurrencyRate = euroCurrencyRate;
	}

	public Double getGoldCurrencyRate() {
		return goldCurrencyRate;
	}

	public void setGoldCurrencyRate(Double goldCurrencyRate) {
		this.goldCurrencyRate = goldCurrencyRate;
	}
	

}
